# import os
# import requests
# from bs4 import BeautifulSoup
# base_path = os.path.dirname(os.path.abspath(__file__))
# img_path = os.path.join(base_path, 'img')
# response = requests.get('http://pic.yesky.com/c/6_20491_1.shtml')
# soup = BeautifulSoup(response.text, 'html.parser')
# div_obj = soup.find(name='div', attrs={'class': 'lb_box'})
# list_dd = div_obj.find_all(name='dd')
# for dd in list_dd:
#     a_obj = dd.find('a')
#     dir_path = os.path.join(img_path, a_obj.text)
#     if not os.path.isdir(dir_path):
#         os.mkdir(dir_path)
#     a_response = requests.get(a_obj.get('href'))
#     a_response.encoding = 'GBK'
#     soup2 = BeautifulSoup(a_response.text, 'html.parser')
#     div_obj2 = soup2.find(name='div', attrs={'class': 'overview'})
#     img_list = div_obj2.find_all(name='img')
#     for img in img_list:
#         img_src = img.get('src')
#         img_response = requests.get(img_src)
#         file_path = os.path.join(dir_path, img_src.rsplit('/', 1)[-1])
#         with open(file_path, 'wb') as f:
#             f.write(img_response.content)

# import requests
# import re
# import parsel
# import csv
# url = 'https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6'
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
#     'Cookie': 'PC_TOKEN=56acebb003; SUB=_2AkMQZPDyf8NxqwFRmf4XzG7jbo9-zw_EieKmOAEpJRMxHRl-yT9kqmMBtRB6O-TeHZuxtT8UZqw1sWOhePlJmZYij8Kt; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WFn8kh0T0UU6726FLnSMwdd; XSRF-TOKEN=vJQRzCbKSfGH5GyielTrC6A8; WBPSESS=UpRpmiZuabnjxMaPsrZiV8mI2aSltpEjS1i-ewThzWbdWlxWt7mFXo_iKbz2bakTR5fSWhTsz6IKwa6ZqAHwWpB_GBOWh-xULcSw_FNbM3nh_6N261LJfmxbaUL4cTab'
# }
# responst = requests.get(url=url, headers=headers)
#
# selector = parsel.Selector(responst.text)
# trs = selector.css('#pl_top_realtimehot tbody tr')
# num = 1
# # csv 配置项
# f = open(f'D:\热搜1.csv', mode='a', encoding='utf-8', newline='')
# csv_write = csv.DictWriter(f, fieldnames=['排序', '标题', '热度'])
# for tr in trs:
#     title = tr.css('.td-02 a::text').get()
#     hot = tr.css('.td-03 .icon-txt::text').get()
#     num += 1
#     dit = {
#         '排序': num,
#         '标题': title,
#         '热度': hot
#     }
#     # csv保存
#     csv_write.writerow(dit)

import requests
import re
import parsel
import csv
url = 'https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
    'Cookie': 'PC_TOKEN=56acebb003; SUB=_2AkMQZPDyf8NxqwFRmf4XzG7jbo9-zw_EieKmOAEpJRMxHRl-yT9kqmMBtRB6O-TeHZuxtT8UZqw1sWOhePlJmZYij8Kt; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WFn8kh0T0UU6726FLnSMwdd; XSRF-TOKEN=vJQRzCbKSfGH5GyielTrC6A8; WBPSESS=UpRpmiZuabnjxMaPsrZiV8mI2aSltpEjS1i-ewThzWbdWlxWt7mFXo_iKbz2bakTR5fSWhTsz6IKwa6ZqAHwWpB_GBOWh-xULcSw_FNbM3nh_6N261LJfmxbaUL4cTab'
}
responst = requests.get(url=url, headers=headers)
selector = parsel.Selector(responst.text)
trs = selector.css('#pl_top_realtimehot tbody tr')
num = 1
f = open(f'D:\热搜.csv', mode='a', encoding='utf-8', newline='')
csv_write = csv.DictWriter(f, fieldnames=['排序', '标题', '热度'])
for tr in trs:
    title = tr.css('.td-02 a::text').get()
    hot = tr.css('.td-03 .icon-txt::text').get()
    num += 1
    dit = {
        '排序': num,
        '标题': title,
        '热度': hot
    }
    csv_write.writerow(dit)
print("爬虫完成")




